package com.aner.test.exception.handler;

import com.aner.test.exception.CustomException;
import com.aner.test.domain.CommonResult;
import com.aner.test.security.exception.UserPasswordNotMatchException;
import org.slf4j.Logger;
import com.aner.tester.utils.LoggerUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * exception handler
 * 
 * @author aner
 */
@RestControllerAdvice
public class CommonExceptionHandler
{
    private static final Logger log = LoggerUtils.getLogger(CommonExceptionHandler.class);

    @ExceptionHandler(UserPasswordNotMatchException.class)
    public CommonResult handleUserPasswordNotMatchException(UserPasswordNotMatchException e)
    {
        log.error(e.getMessage(), e);
        return CommonResult.error(e.getMessage());
    }

    /**
     * custom validation exception
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public CommonResult validatedCustomException(CustomException e)
    {
        log.error(e.getMessage(), e);
        return CommonResult.error(e.getMessage());
    }

    /**
     * all validation exception
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public CommonResult validatedException(Exception e)
    {
        log.error(e.getMessage(), e);
        return CommonResult.error(e.getMessage());
    }

}
